<template>
  <div class="box-yuyi">
    <div class="box-left">
      <container-nav :treeData="treeData"
                     :setLabelId="setLabelId"
                     :loading="treeLoading"
                     @handleNodeClick="handleNodeClick">
      </container-nav>
    </div>
    <div class="box-content">
      <div class="head-search">
        <search @onSubmit="onSearch">
          <el-form-item label="方案名称">
            <el-input placeholder="请输入内容"
                      clearable
                      v-model="searchData.fmtName"
                      size="small"></el-input>
          </el-form-item>
        </search>
        <el-button class="reset" size="small" icon="el-icon-refresh" @click="handleReset">重置</el-button>
      </div>

      <div class="fun-btn">
        <el-button @click="handleAdd('add')" size="small" type="primary" icon="el-icon-circle-plus-outline">新增
        </el-button>
        <el-button type="danger" plain size="small" icon="el-icon-delete" @click="handleDel">删除</el-button>
      </div>

      <section class="list-box">
        <el-table
          :data="tableData.list"
          border
          v-loading="loading"
          height="calc(100vh - 252px)"
          @selection-change="handleSelectionChange"
          class="table">
          <el-table-column
            type="selection"
            align="center"
            fixed="left"
            width="60">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            align="center"
            width="60">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="fmtName"
            label="方案名称">
          </el-table-column>
          <el-table-column
            show-overflow-tooltip
            prop="fmtPerview"
            minWidth="300"
            label="格式预览">
          </el-table-column>
          <el-table-column
            prop="isDefault"
            width="120"
            label="是否默认">
            <template slot-scope="scope">
              <div style="text-align: center">
                <el-switch v-model="scope.row.isDefault" @change="handleSwitch(scope.row)"></el-switch>
              </div>
            </template>
          </el-table-column>
          <el-table-column
            fixed="right"
            align="center"
            label="操作"
            width="150">
            <template slot-scope="scope">
              <div style="text-align: center">
                <el-button type="text"
                           icon="el-icon-edit"
                           @click="handleAdd('edit', scope.row)">编辑
                </el-button>
                <el-button v-if="!scope.row.isDefault"
                           type="text"
                           @click="handleAdd('relation', scope.row)">关联全宗
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.pageNo"
          :page-sizes="[20, 40, 60, 80]"
          :page-size="page.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="tableData.total">
        </el-pagination>
      </section>

      <el-dialog :title="dialogTitle"
                 :width="dialogWidth"
                 top="5vh"
                 :close-on-click-modal="false"
                 :close-on-press-escape="false"
                 :before-close="(done)=>{tableRowData={}; done()}"
                 :visible.sync="dialogShow">
        <div class="import-content">
          <add-programme v-if="type !== 'relation'"
                         :idCategory="idCategory"
                         :tableId="tableId"
                         :editData="tableRowData"
                         :type="type"
                         :options="options"
                         :leftData="leftData"
                         ref="add"
                         @handleSuccess="handleSuccess">
          </add-programme>
          <relation-quanzong v-if="type === 'relation'"
                             :tableRowData="tableRowData"
                             @handleSuccess="handleRelationSuccess"></relation-quanzong>
        </div>
      </el-dialog>

    </div>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import ContainerNav from '../../../../components/containerNav/containerNav'
import Search from '../../../../components/search/search'
import AddProgramme from '../settingComponents/reference/addProgramme'
import RelationQuanzong from '../settingComponents/reference/relationQuanzong'
import { delReferenceDic, editReferenceDic, getReferenceDic } from '../../../../api/reference/reference'
import { getBusinessDic, getCategoryTree } from '../../../../api/category/category'
import { getSysDicId } from '../../../../api/dictionary/dictionary'

export default {
  name: 'Reference',
  components: {
    RelationQuanzong,
    AddProgramme,
    Search,
    ContainerNav
  },
  data () {
    return {
      treeLoading: true,
      treeData: [],
      setLabelId: {
        label: 'name',
        value: 'id',
        children: 'children'
      },
      searchData: {
        fmtName: ''
      },
      dialogTitle: '',
      dialogWidth: '',
      dialogShow: false,
      type: '',
      page: {
        pageNo: 1,
        pageSize: 20
      },
      tableData: {},
      loading: false,
      idCategory: '',
      tableSelected: [],
      tableId: '', //
      tableRowData: {},
      options: [],
      leftData: [] // 新增、编辑左边列表
    }
  },
  mounted () {
    // 默认收起左侧导航栏
    this.setNavNarrow(true)
    this.getTreeList()
  },
  methods: {
    ...mapActions('profile/navNarrow', ['setNavNarrow']),
    getTreeList () {
      getCategoryTree('sysModel', this.$route.query.isNotAjJnNode).then(res => {
        this.treeData = res.data.data
        this.treeLoading = false
      })
    },
    getList () {
      this.loading = true
      getReferenceDic(this.page).then((res) => {
        this.tableData = res.data.data
        this.loading = false
      }).catch(() => {
        this.tableData = {
          list: [],
          total: 0
        }
        this.loading = false
      })
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.page.pageSize = val
      this.getList()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.searchData.pageNo = val
      this.getList()
    },
    handleNodeClick (data) {
      this.page.pageSize = 20
      this.page.pageNo = 1
      this.idCategory = data.idCategory
      this.$set(this.page, 'params', [{
        op: 'eq',
        name: 'idCategory',
        value: data.idCategory
      }])
      data.table.map(item => {
        if (item.tableType === 'aj' || item.tableType === 'jh') {
          this.tableId = item.idTable
        }
      })
      this.getList()
    },
    onSearch () {
      // 搜索
      this.page.pageSize = 20
      this.page.pageNo = 1
      if (this.searchData.fmtName) {
        this.page.params.push({
          op: 'like',
          name: 'fmtName',
          value: this.searchData.fmtName
        })
      } else if (!this.searchData.fmtName && this.page.params.length > 1) {
        this.page.params.splice(1, 1)
      }
      this.getList()
    },
    handleReset () {
      // 重置
      this.searchData.fmtName = ''
    },
    handleSelectionChange (selection) {
      // 勾选表格行
      this.tableSelected = []
      selection.map(item => {
        this.tableSelected.push(item.id)
      })
    },
    handleAdd (type, row) {
      // 新增
      if (!this.page.params) {
        this.$message.warning('请选择左侧门类进行新增操作！')
        return
      }
      const title = {
        add: '新增',
        edit: '编辑',
        relation: '关联全宗'
      }
      this.tableRowData = row
      this.type = type
      this.dialogShow = true
      this.dialogTitle = title[type]
      this.dialogWidth = type === 'relation' ? '600px' : '90%'
      if (type === 'add' || type === 'edit') {
        getBusinessDic(this.tableId).then(res => {
          this.options = res.data.data
        })
        getSysDicId('sys_archive_no_type').then(res => {
          this.leftData = res.data.data
        })
      }
    },
    handleSuccess () {
      // 新增成功
      this.dialogShow = false
      this.getList()
    },
    handleSwitch (row) {
      // 修改
      editReferenceDic(row).then(() => {
        this.$message.success('修改成功')
        this.getList()
      })
    },
    handleRelationSuccess () {
      // 关联全宗成功
      this.dialogShow = false
      this.getList()
    },
    handleDel () {
      // 删除
      if (this.tableSelected.length < 1) {
        this.$message.warning('请选择最少一条信息进行删除操作！')
      } else {
        this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delReferenceDic(this.tableSelected).then(() => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.getList()
          })
        }).catch(() => {
        })
      }
    }
  }
}
</script>

<style lang="scss" scoped>
  .head-search {
    padding-top: 10px;

    ::v-deep .search-btn {
      margin-top: 0;
    }

    .reset {
      margin-top: 6px;
    }
  }

</style>
